<template>
    <div id="app">
        <headerView></headerView>
        <!-- <bread></bread> -->
        <div class="main-whole"><router-view></router-view></div>
        <footerView></footerView>
        <login v-if="login"></login>
        <register v-if="reg"></register>
        <forgot v-if="forgot"></forgot>
        <affix></affix>
    </div>
</template>
<script>
    import headerView from '../../components/header.vue';
    // import bread from './components/bread.vue';
    import footerView from '../../components/footer.vue';
    import login from '../../pages/user/login.vue';
    import forgot from '../../pages/user/forgot.vue';
    import register from '../../pages/user/register.vue'
    import { mapState } from 'vuex';
    import affix from '../../components/affix.vue'
    export default {
        components:{
            headerView,
            footerView,
            login,
            register,
            forgot,
            affix
          },
        data () {
            return {
                loading: false,
                user_id:'',
                depth:0,
                categoryList:[]
            }
        },
        computed: mapState({
            spinShow: 'mainLoading',
            login:'loginState',
            reg:'regState',
            token:'token',
            forgot:'forgotState'
        }),
        mounted() {
            this.getCarousel();
            if(this.token){
                this.getAuth();
            }
        },
        methods:{
            // 获取当前登录用户
            getAuth() {
                this.$http.get('/member/me?include=statute,term,questions&fe=1').then(res => {
                    this.$store.dispatch('setUserInfo', res.data.data);
                }).catch(err => {
                    this.$Notice.open({
                        title: '请登录..',
                    });
                    // this.$Message.error( '请登录..');
                })
            },
            getCarousel() {
                this.$http.get('/category/list',{
                    params:{
                        include:"childrens.childrens.childrens",
                        fe:1
                    }
                })
                .then(res => {
                    this.categoryList = res.data.data;
                    this.getTree(this.categoryList);
                    this.$store.dispatch('setLawList',{
                        depth:this.depth+1,
                        name:'注册法规',
                        children:this.categoryList
                    });
                }).catch(err => {
                    this.$Notice.open({
                        title: 'category',
                    });
                    // this.$Message.error( 'category');
                })
            },
            getTree(node){
                if(node && node.length != 0){
                    node.forEach((item,index)=>{
                        item.label = item.name;
                        item.value = item.id;
                        this.depth = this.depth > item.depth ? this.depth : item.depth;
                        if(item.childrens){
                            item.children = item.childrens.data;
                            this.getTree(item.childrens.data);
                            delete item.childrens;
                        }
                    })
                }
            }
        }
    }
</script>